<?php
// Date in the past 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1 
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0 
header("Pragma: no-cache");

include_once('mq.php');
if (session_id() == '')
    session_start();

if (isset($_GET['logout']))
    MQ_Logout();
if (!empty($_SESSION['mq_l'])) {
    $login = substr((int) $_SESSION['mq_l'], 0, 14);
    $password = substr($_SESSION['mq_p'], 0, 16);

    $res = MQ_Query('USERINFO-login=' . $login . '|password=' . $password
            , T_CACHEDIR
            , T_CACHETIME
            , $login . '/');

    if ($res == '!!!CAN\'T CONNECT!!!') {
        $this->_CONNECTION_TIMEOUT = true;
        exit;
    }
}
if (empty($res) || strpos($res, 'Invalid') !== false || strpos($res, 'Disabled') !== false) {
    MQ_Logout();
    exit;
}
?>
<html>
    <head>
        <title>Account information::Trade</title>
    </head>
    <body>
        <table cellspacing="0" cellpadding="0" border="0" width="100%" style="background-color: buttonface;">
            <tr bgcolor="#ffffff"><td align="right" style="font-size:12px; font-family:Tahoma, Arial, Helvetica, sans-serif;">
                    <?php
                    $info = explode("\r\n", $res);
                    $size = sizeof($info);

                    $account = $info[0];
                    $name = $info[1];
                    ?>
            <nobr><b>Account:</b> <?= $account ?>
                &nbsp;|&nbsp; <b>Name:</b> <?= $name ?>
                &nbsp;|&nbsp; <a href="?logout=1" onCLick="return confirm('Are you sure?');">Logout</a></nobr><br>
            <img src="img/0.gif" width="1" height="5"/>
        </td></tr>
    <tr><td>
            <table cellspacing="1" cellpadding="3" border="0" width="100%" style="font-size:12px; font-family:Tahoma, Arial, Helvetica, sans-serif;">
                <tr align="right" style="background-color: #a0a0a0;">
                    <td align="left"><b>Order</b></td>
                    <td><b>Time</b></td>
                    <td><b>Type</b></td>
                    <td><b>Lots</b></td>
                    <td><b>Symbol</b></td>
                    <td><b>Price</b></td>
                    <td><b>S/L</b></td>
                    <td><b>T/P</b></td>
                    <td><b>Price</b></td>
                    <td><b>Comm</b></td>
                    <td><b>Swap</b></td>
                    <td><b>Profit</b></td>
                </tr>
                <?php
                $beginIndex = 3;


                $balance = MQ_GetParam($info[$beginIndex]);
                $equity = MQ_GetParam($info[$beginIndex + 1]);
                $margin = MQ_GetParam($info[$beginIndex + 2]);
                $free_margin = MQ_GetParam($info[$beginIndex + 3]);
                $margin_level = $margin != 0 ? number_format(100 * ($equity / $margin), 2, '.', '') . '%' : '0%';

                $cnt = 0;
                for ($i = $beginIndex + 4; $i < $size; $i++) {
                    if ($info[$i] === '0')
                        break;
                    $row = explode("\t", $info[$i]);
                    ?>
                    <tr align="right" bgcolor="<?= ($cnt % 2 ? '#e0e0e0' : '#ffffff') ?>">
                        <td align="left"><?= $row[0] ?></td>
                        <td><?= $row[1] ?></td>
                        <td><?= $row[2] ?></td>
                        <td><nobr><?= $row[3] ?></nobr></td>
            <td><?= strtolower($row[4]) ?></td>
            <td><nobr><?= $row[5] ?><nobr></td>
                <td><nobr><?= $row[6] ?><nobr></td>
                        <td><nobr><?= $row[7] ?><nobr></td>
                                <td><nobr><?= $row[8] ?><nobr></td>
                                        <td><nobr><?= $row[9] ?><nobr></td>
                                                <td><nobr><?= $row[10] ?><nobr></td>
                                                        <td><nobr><?= $row[11] ?><nobr></td>
                                                                </tr>
    <?php
    ++$cnt;
}

$profit = MQ_GetParam($info[$i + 3]) + MQ_GetParam($info[$i + 1]) + MQ_GetParam($info[$i + 2]);
?>
                                                            <tr bgcolor="#cccccc">
                                                                <td colspan="11">
                                                                    <b>Balance: <nobr><?= $balance ?></nobr>
                                                                        Equity: <nobr><?= $equity ?></nobr>
                                                                        Margin: <nobr><?= $margin ?></nobr>
                                                                        Free margin: <nobr><?= $free_margin ?></nobr>
                                                                        Margin level: <nobr><?= $margin_level ?></nobr></b></td>
                                                                <td align="right" class="room_text"><b><?= $profit ?></b></td>
                                                            </tr>
<?php
$cnt = 0;
for ($i+=4; $i < $size; $i++) {
    if ($info[$i] === '0')
        break;
    $row = explode("\t", $info[$i]);
    ?>
                                                                <tr align="right" bgcolor="<?= ($cnt % 2 ? '#e0e0e0' : '#ffffff') ?>">
                                                                    <td align="left"><?= $row[0] ?></td>
                                                                    <td><?= $row[1] ?></td>
                                                                    <td><?= $row[2] ?></td>
                                                                    <td><nobr><?= $row[3] ?></nobr></td>
                                                                <td><?= strtolower($row[4]) ?></td>
                                                                <td><nobr><?= $row[5] ?><nobr></td>
                                                                        <td><nobr><?= $row[6] ?><nobr></td>
                                                                                <td><nobr><?= $row[7] ?><nobr></td>
                                                                                        <td><nobr><?= $row[8] ?><nobr></td>
                                                                                                <td colspan="3">&nbsp;</td>
                                                                                                </tr>
    <?php
    ++$cnt;
}
?>
                                                                                            </table>
                                                                                            </td></tr>
                                                                                            <tr bgcolor="#ffffff"><td style="font-size:12px; font-family:Tahoma, Arial, Helvetica, sans-serif;">
                                                                                                    <img src="img/0.gif" width="1" height="5"/><br>
                                                                                            <nobr><b>Trade</b> &nbsp;|&nbsp; <a href="history.php">Account history</a></nobr>
                                                                                            </td></tr>
                                                                                            </table>
                                                                                            </body>
                                                                                            </html>